package com.wen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wen.pojo.Orders;
import com.wen.pojo.Orders;
import com.wen.pojo.dto.*;
import com.wen.pojo.dto.wen.*;
import org.apache.ibatis.annotations.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.wen.pojo.*;
import com.wen.pojo.dto.OrdersDto;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.pojo.Orders;
import com.wen.pojo.dto.ProductAndOrdersAndOrdersDetails;
import com.wen.pojo.dto.ProductAndOrdersAndOrdersDetailsDto;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface OrdersMapper extends BaseMapper<Orders> {

    @Select("SELECT * FROM orders o,consumer c WHERE o.`consumer_id` = c.`consumer_id` AND c.`merchant_tag` = 's001-4'")
    List<OrdersDto> sel();

    Page<OrdersDto> sel(Page page, @Param("dto") OrdersDtoTiaoJian dto);

    Page<OrdersANDConsumerANDMerchants> selAll(Page page, @Param("dto") OrdersANDConsumerANDMerchantDto dto);

    @Select("SELECT * FROM `orders` o,`order_details` od,`merchant` m WHERE o.`merchant_id` = m.`merchant_id` AND od.`orders_id` = o.`id` AND o.`orderState` = 1 ")
    Page<DiaoBos> seldb(Page page);

    IPage<TransferGoods> searchPage2(Page<TransferGoods> pageParam, @Param("po") TransferGoodsDto dto);

    @Select("select o.id as orderId,o.order_number as orderNumber,o.totalNum as totalPrice,sum(d.amount) as amout from orders o,order_details d where o.id=d.orders_id and  o.id=#{id}")
    Orders2 ordersByid(String id);

    @Select("select o.id as id,p.picture as imgLink,p.unit as unit,p.name as name,o.amount as amount,o.price as price,o.subtotal as subtotal,p.id as productid from order_details o,product p where o.product_id=p.id and o.orders_id=#{id}")
    List<OrderDetails2> OrderDetailslist(String id);

    @Insert("insert into pay values (null,#{payOrderNumber},#{stockId},#{payer},#{reason},#{totalAmount},#{totalPrice},#{orderList},#{register},#{registerTime},#{checker},#{checkTime},#{checkReason},#{checkTag})")
    @Options(useGeneratedKeys = true, keyProperty = "payId")
    boolean addPay(Pay pay);

    @Select("select *from stock_details")
    List<StockDetails> stocklist();

    @Insert("insert into pay_details values (null,#{payId},#{stockId},#{productId},#{amount},#{price},#{subTotal})")
    boolean addPayDetails(PayDetails pay);

    @Update("update stock_details set real_amount=real_amount-#{amount} where id=#{stockId}")
    void upStock(PayDetails payDetails);

    @Update("update stock_details set real_amount=real_amount+#{amount} where id=#{stockId}")
    void upStock2(PayDetails payDetails);

    @Update("update orders set orderState = 2 where id = #{id}")
    void upOrderState(String id);

    @Select("SELECT o.*,c.`nick_name` FROM `orders` o,`consumer`c WHERE o.`consumer_id` = c.`consumer_id` and o.orderState = 1")
    IPage<OrdersDto> selOrderState1(Page page);

    @Select("SELECT o.*,c.`nick_name` FROM `orders` o,`consumer`c WHERE o.`consumer_id` = c.`consumer_id` and o.orderState = 2")
    IPage<OrdersDto> selOrderState2(Page page);

    @Select("SELECT o.*,c.`nick_name` FROM `orders` o,`consumer`c WHERE o.`consumer_id` = c.`consumer_id` and o.orderState = 3")
    IPage<OrdersDto> selOrderState3(Page page);

    @Update("update orders set orderState = 3 where id = #{id}")
    Boolean upOrderState3(String id);


    Page<ProductAndOrdersAndOrdersDetails> selectAll(Page page, @Param("order") ProductAndOrdersAndOrdersDetailsDto dto);


    Page<OrderAll> getOrderList(Page page, @Param("order") ProductAndOrdersAndOrdersDetailsDto dto);
//    List<OrderDetails_product> orderList;


//
//    @Select(" SELECT *,od.`id` AS ids, p.`id` AS pid FROM orders o\n" +
//            " LEFT JOIN order_details od ON o.id = od.`orders_id`\n" +
//            " LEFT JOIN product p  ON od.`product_id` = p.`id`\n" +
//            " WHERE o.`id` = #{id}")
//    List<ProductAndOrdersAndOrdersDetails>   selectOrderById(Integer id);


    List<OrderAll> selectOrderById(Integer id);

}
